package com.conversationboard.view;

import java.security.Principal;
import java.sql.SQLException;

import com.conversationboard.config.Configuration;
import com.conversationboard.model.Board;
import com.conversationboard.model.User;
import com.conversationboard.site.Site;

public class MenuDisplay {

	public static String display(Board board, boolean showBoardName, Principal principal) throws SQLException {

		boolean loggedIn = (principal != null);
		User user = null;
		StringBuilder buffer = new StringBuilder();

		if (showBoardName) {
			buffer.append(board.getName());
			buffer.append(": ");
			buffer.append(board.getDescription());
		}

		if (loggedIn) {
			user = User.get(principal.getName());
		}

		buffer.append("<div class='menubar'>");

		buffer.append("<a class='highlightmenubuttongreen' href='" + Configuration.getInstance().getRoot() + "/SiteNewAnswersControllerServlet'>Site New " + Site.getPost() + "s</a> ");
		buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/Search/search.jsp'>");
		buffer.append("Search");
		buffer.append("</a> ");

		buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/index.jsp'>Boards</a> ");

		if (loggedIn) {
			buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/PrePreferencesControllerServlet'>Preferences</a> ");
			buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/BookmarksControllerServlet'>Bookmarks</a> ");

			if (Configuration.getInstance().isUsersCanSeeTheirRecentPosts()) {
				buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/FullTextSearchControllerServlet?terms=" + user.getLoginId() + "&offset=0&searchtype=id&startdate=&enddate=&artefact=messages&idtype=loginid&sortorder=Descending&boardid=0'>My Recent Posts</a> ");
			}
		}

		/* Add links to popular boards */

		MainMenu.displayBoardShortcuts(user, buffer, board.getBoardId());

		buffer.append("<a class='highlightmenubuttongreen' href='" + Configuration.getInstance().getRoot() + "/NewAnswersControllerServlet?boardid=");
		buffer.append(board.getBoardId());
		buffer.append("'>");
		buffer.append(board.getShortName() + " " + Site.getPost() +"s");
		buffer.append("</a> ");

		buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/AskNewQuestionControllerServlet?boardid=");
		buffer.append(board.getBoardId());
		buffer.append("'>");
		buffer.append("New " + Site.getThread());
		buffer.append("</a> ");

		buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/RecentQuestionsControllerServlet?boardid=");
		buffer.append(board.getBoardId());
		buffer.append("&amp;offset=0");
		buffer.append("'>");
		buffer.append("New " + Site.getThread() + "s");
		buffer.append("</a> ");

		if (board.isAllowPolls()) {
			buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/EnterPollInformationControllerServlet?boardid=");
			buffer.append(board.getBoardId());
			buffer.append("'>");
			buffer.append("New Poll");
			buffer.append("</a> ");
		}

		if (Configuration.getInstance().isEnableBlogView()) {
			buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/BlogViewControllerServlet?boardid=");
			buffer.append(board.getBoardId());
			buffer.append("'>");
			buffer.append("Blog View");
			buffer.append("</a> ");
		}

		if (loggedIn) {
			buffer.append("<a class='menubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/confirmlogout.jsp'>Logout</a> ");
		} else {
			buffer.append("<a class='highlightmenubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/register.jsp'>Register</a> ");
			buffer.append("<a class='highlightmenubutton' href='" + Configuration.getInstance().getRoot() + "/Pages/login.jsp'>Login</a> ");
		}

		if (loggedIn && user.isAdmin()) {
			buffer.append("<a href='#' class='menubutton' onClick='setStyleByClass(\"div\",\"adminlinks\",\"display\", \"inline\");'>");
			buffer.append("Admin");
			buffer.append("</a> ");
		}

		buffer.append("</div>");

		return buffer.toString();
	}

}
